//
//  YSZComment.swift
//  GZWeibo666
//
//  Created by ysz on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class YSZComment: NSObject
{
    ///评论创建的时间
    var created_at: String?
    
    ///评论的id
    var id: Int = 0
    
    ///评论的内容
    var text: String?
    
    ///用户信息
    var user: CZUser?
    
    ///模型数组
    var result: [YSZComment]?
    
    
    ///字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        //字典转模型
        setValuesForKeysWithDictionary(dict)
    }
    //设置user模型
    override func setValue(value: AnyObject?, forKey key: String)
    {
        if key == "user"
        {
            if let dict = value as? [String : AnyObject]
            {
                 user = CZUser(dict: dict)
            }
           return
        }
        super.setValue(value, forKey:key)
        
    }
    //没有对应属性调这个空方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    ///打印
    override var description: String {
        let properties = ["id","text","created_at", "user"]
        
        return "\n\t:评论模型: \(dictionaryWithValuesForKeys(properties))"
    }
    /**
    加载评论
    
    - parameter id:       微博的id
    - parameter finished: 回调
    - parameter error:    错误
    */
    class func loadComment(id: Int ,since_id: Int, max_id: Int, finished: (comment: [YSZComment]?,error: NSError?)-> ())
    {
        //从网络工具类里面获取数据
        CZNetworkTool.sharedInstance.loadComment(id,since_id: since_id,max_id: max_id) { (result, error) -> () in
            if error != nil || result == nil
            {
                print("\(error)")
                finished(comment: nil, error: error)
                return
            }
            
            if let array = result!["comments"] as? [[String: AnyObject]]
            {
                var comments = [YSZComment]()
                
                for dict in array
                {
                    let comment = YSZComment(dict: dict)
                    comments.append(comment)
                }
                finished(comment: comments, error: nil)
            }
            
        }
    }
    

}
